package com.yuanfengerp.logistics.service.impl;


import com.yuanfengerp.logistics.dao.ILogisticsMapper;
import com.yuanfengerp.logistics.moudel.LogisticsInfo;
import com.yuanfengerp.logistics.moudel.LogisticsManageInfo;
import com.yuanfengerp.logistics.service.ILogisticsService;
import com.yuanfengerp.publicUtil.Result;
import com.yuanfengerp.publicUtil.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

@Service

public class ILogisticsServiceImpl implements ILogisticsService {
    @Autowired
    private ILogisticsMapper ilm;
    @Override
    public Result<String> insertLogisticsManage(LogisticsInfo logistics) {
        SnowflakeIdWorker idWorker = new SnowflakeIdWorker(1);
        logistics.setLogistics_document_ID(idWorker.nextId());
        logistics.setLogistics_operate_time(new Timestamp(System.currentTimeMillis()));
        logistics.setLogistics_delivery_time(new Timestamp(System.currentTimeMillis()));
        logistics.setLogistics_state(0);

        int i = ilm.insertLogisticsManage(logistics);
        if (i > 0) {
            // 如果插入成功，继续处理连表新增的费用项信息插入
            List<LogisticsManageInfo> logisticsManageInfos = logistics.getLogisticsManageInfoList();
            if (logisticsManageInfos != null && !logisticsManageInfos.isEmpty()) {
                for (LogisticsManageInfo logisticsManageInfo : logisticsManageInfos) {
                    int j = ilm.insertLogisticsManageInfo(logisticsManageInfo);
                    if (j <= 0) {
                        // 如果插入连接表失败，需要进行相应处理，这里简单返回失败结果
                        return Result.failure(500, "添加费用项到连接表失败");
                    }
                }
            }


            // 如果所有操作都成功，返回成功结果
            return Result.success("添加成功");
        } else {
            // 如果插入失败，返回失败结果并附带错误信息
            return Result.failure(500, "添加失败，数据库插入操作未成功");
        }
    }

    @Override
    public Result<String> setState(String logistics_ID, Integer logistics_state) {
        int i = ilm.setState(logistics_ID, logistics_state);
        return i > 0 ? Result.success("添加成功") : Result.failure(400, "添加失败");
    }

    @Override
    public Result<List<LogisticsInfo>> getAllLogisticsManage() {
        List<LogisticsInfo> logistics = ilm.getAllLogisticsManage();
        return Result.success(logistics);
    }

    @Override
    public Result<List<LogisticsInfo>> getLogisticsManage(Map<String, Object> params) {
        params.put("pageSize", ((int) params.get("pageSize") - 1) * (int) params.get("pageNum"));
        List<LogisticsInfo> logisticsInfos = ilm.getLogisticsManage(params);
            if ((logisticsInfos.isEmpty()))
                return Result.failure(400, "没有数据");
            if (params == null)
                return Result.failure(400, "参数为空");
            return Result.success(logisticsInfos);
    }

    @Override
    public Result<String> deleteLogisticsManage(String logistics_ID) {
        int i = ilm.deleteLogisticsManage(logistics_ID);
        return i > 0 ? Result.success("删除成功") : Result.failure(400, "删除失败");
    }

    @Override
    public Result<String> updateLogisticsManage(LogisticsInfo logistics) {
        logistics.setLogistics_delivery_time(new Timestamp(System.currentTimeMillis()));
        int i = ilm.updateLogisticsManage(logistics);
        return i > 0 ? Result.success("修改成功") : Result.failure(400, "修改失败");
    }

    @Override
    public Result<LogisticsInfo> selectLogisticsManage(String logistics_ID) {
        LogisticsInfo logisticsInfo = ilm.selectLogisticsManage(logistics_ID);
        if (logisticsInfo == null)
            return Result.failure(400, "没有数据");
        return Result.success(logisticsInfo);
    }

    @Override
    public Result<List<LogisticsInfo>> selectLogisticsManageInfo(String logistics_ID) {
//        List<LogisticsInfo> logisticsInfos = ilm.selectLogisticsManageInfo(logistics_ID);
        System.out.println(logistics_ID);
        List<LogisticsInfo> logisticsInfos = ilm.selectLogisticsManageInfo((String) logistics_ID);
        if (logisticsInfos.isEmpty())
            return Result.failure(400, "没有数据");
        return Result.success(logisticsInfos);
    }
}
